package com.echo.code.thread.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupportBlockerExam Created by baiyap on 2018/10/10.
 */
public class LockSupportBlockerExam {

  public static void main(String[] args) {
    Thread t3 = new Thread(() -> {
      System.out.println(Thread.currentThread().getName() + " park 5 seconds");
      // park 5 seconds, set blocker
      Object blocker = new String("Alex Wang");
      LockSupport.parkUntil(blocker,
          System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
      System.out.println(Thread.currentThread().getName() + " after park");
    }, "t3");
    t3.start();

    try {
      Object t3_blocker = null;
      while (t3_blocker == null) {
        t3_blocker = LockSupport.getBlocker(t3);
        TimeUnit.MILLISECONDS.sleep(10);
      }
      System.out.println("t3 blocker is :" + t3_blocker);
      t3.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
